namespace FluentDataContract.Attributes
{
    using System;
    using Internal;

    public class AttributedDataContractFactory
    {
        private readonly InstanceCache _cache = new InstanceCache();

        public virtual IObjectContract GetObjectContract(Type type)
        {
            if (type == null)
                return null;

            var attribute = (DataContractAttribute)Attribute.GetCustomAttribute(type, typeof(DataContractAttribute));

            if (attribute == null || attribute.ObjectContractType == null)
                return null;

            var result = (_cache.GetOrCreateInstance(attribute.ObjectContractType) as IHasAContract<IObjectContract>).Contract;
            return result;
        }
    }
}